<?php
namespace app\admin\controller;
use think\Request;

class Jjcc extends Base
{
    
    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/jjcc/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account']])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/jjcc/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
   
    //红包记录
    public function money_list($uid)
    {
        $post = request()->only(['type'],'post');
        
        if (!empty($post['type'])){
            $where['type'] = $post['type'];
        }
        $where['admin_id'] = $this->accountId;
        $where['user_id'] = $uid;
 
        $res = db('envelopes_water')->where($where)->order('create_time desc')->paginate($this->page, false);

        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        $this->assign('uid', $uid);
        return $this->fetch();
        
    }
    
    //提现确定
    public function money_list_edit($id=null,$type,$user_id)
    {
        if ($type == 1){
            $res = db('envelopes_water')->where(['id'=>$id])->update(['sort'=>2]);
        }elseif($type == 2){
            $res = db('envelopes_water')->where(['user_id'=>$user_id])->update(['sort'=>2]);
        }

        if ($res) {
            $this->success('操作成功','jjcc/user_list' );
        }else{
            $this->error('操作失败');
        }
    }
    
    //会员列表
    public function user_list()
    {
        $post = request()->only(['name','money'],'post');
        
        if (!empty($post['name'])){
            $where['b.nickname'] = ['like','%'.$post['name'].'%'];
        }else{
            $where['b.nickname'] = ['neq',''];
        }
        
        if (!empty($post['money'])){
            $where['c.money'] = ['gt',$post['money']];
        }
        $field = "a.id,a.user_id,a.challenge_number,a.share_number,a.award_number,a.create_time,b.avatarurl,b.nickname,c.money";
        $where['a.admin_id'] = $this->accountId;
        
        $where['b.avatarurl'] = ['neq',''];
        $res = db('jjcc_personal')
                ->alias('a')
                ->join('user b','a.user_id = b.id','left')
                ->join('envelopes c','a.user_id = c.user_id','left')
                ->field($field)
                ->where($where)
                ->paginate($this->page, false);
        $number = count($res);

        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        $this->assign('number', $number);
        return $this->fetch();
        
    }
    
    //会员编辑页面
    public function user_list_edit($id)
    {
        $res = db('jjcc_personal')->where(['id'=>$id])->field('id,challenge_number')->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //会员挑战次数编辑
    public function user_edit_post()
    {
        $post = request()->only(['id','challenge_number'],'post');
        
        $res = db('jjcc_personal')->where(['id'=>$post['id']])->update(['challenge_number'=>$post['challenge_number']]);
        
        if ($res) {
            $this->success('编辑成功','jjcc/user_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //配置管理
    public function configure()
    {
        $res = db('jjcc_config')->where(['admin_id'=>$this->accountId])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //编辑配置
    public function configure_post()
    {
        $post = request()->only(['index_game','go_prize','clearance','my_prize','prize_list','index_advertisement','game_all','game_rules','game_content','logo_title',
                                'comegame_title','time_title','game_a','count_down_a','count_down_reduce_a','game_b','count_down_b','count_down_reduce_b',
                                'game_c','count_down_c','count_down_reduce_c','game_d','count_down_d','count_down_reduce_d','wrong_one','wrong_two',
                                'game_ok_title','index_advertisement_imgurl','index_advertisement_url','index_share_title','index_share_buttitle',
                                'share_title','game_number_day','game_number_new','share_day','share_daypu','share_humber','checkpoint_number',
                                'game_ok_one','money_all','money_min','money_max','customer_service'
        ],'post');
        
        //图片上传
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['share_img'] = $image;
        }
        
        $res = db('jjcc_config')->where(['admin_id'=>$this->accountId])->update($post);
        
        if ($res) {
            $this->success('编辑成功','jjcc/configure');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //奖品列表
    public function prize_list()
    {
        $res = db('jjcc_prize')->where(['admin_id'=>$this->accountId])->order('create_time desc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //增加奖品
    public function prize_add()
    {
        return $this->fetch();
    }
    
    //增加奖品提交
    public function prize_add_post()
    {
        $post = request()->only(['name'],'post');
        
        //图片上传
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['image'] = $image;
        }
        
        $post['admin_id'] = $this->accountId;
        $post['create_time'] = date("Y-m-d H:i:s");
        
        $res = db('jjcc_prize')->insert($post);
        
        if ($res) {
            $this->success('编辑成功','jjcc/prize_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //编辑奖品
    public function prize_edit($id)
    {
        $res = db('jjcc_prize')->where(['id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //编辑奖品提交
    public function prize_edit_post()
    {
        $post = request()->only(['id','name'],'post');
        
        //图片上传
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['image'] = $image;
        }
        

        $post['create_time'] = date("Y-m-d H:i:s");
        
        $res = db('jjcc_prize')->where(['id'=>$post['id']])->update($post);
        
        if ($res) {
            $this->success('编辑成功','jjcc/prize_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
    //奖品删除
    public function prize_del($id)
    {
        $res = db('jjcc_prize')->delete($id);
        
        if ($res) {
            $this->success('删除成功','jjcc/prize_list');
        }else{
            $this->error('删除失败');
        }
    }
    
    
    //奖品申请列表
    public function prize_apply_list()
    {
        $res = db('jjcc_address')->alias('a')->join('jjcc_prize b','a.prize_id = b.id','left')
                ->where(['a.admin_id'=>$this->accountId])
                ->field('a.id,b.image,b.name,a.name as names,a.tel,a.address,a.state,a.create_time')
                ->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    
    //发货
    public function prize_apply_edit($id)
    {
        $res = db('jjcc_address')->where(['id'=>$id])->update(['state'=>1]);   
        
        if ($res) {
            $this->success('发货成功','jjcc/prize_apply_list');
        }else{
            $this->error('发货失败');
        }
    }
    
    
    //投诉管理
    public function tscontent_list()
    {
        $res = db('jjcc_complaint')->alias('a')
                ->join('jjcc_tscontent b','a.conetnt = b.id')
                ->join('user c','a.user_id = c.id','left')
                ->where(['a.admin_id'=>$this->accountId])
                ->field('c.nickname,b.title,a.create_time')
                ->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //统计列表
    public function statistical_list()
    {


        $res = db('jjcc_statistics')
                ->field("
                    sum(user_new) as user_new,
                    sum(user_come) as user_come,
                    sum(user_number) as user_number,
                    sum(user_people) as user_people,
                    sum(share_number) as share_number,create_time")
                ->where(['admin_id'=>$this->accountId])->group('create_time')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //更多好玩列表
    public function extension_list()
    {
        $res = db('jjcc_game')->where(['admin_id'=>$this->accountId])->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }

    
    //更多好玩添加页面
    public function extension_add()
    {
        return $this->fetch();
    }

    //更多好玩添加-提交
    public function extension_add_post()
    {
        $post = request()->only(['type','name','title','content','url'],'post');
        
        $post['create_time'] = date("Y-m-d H;i:s");
        $post['admin_id'] = $this->accountId;
        
        //图片上传

        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['image'] = $image;
        }

        $res = db('jjcc_game')->insert($post);
        
        if ($res) {
            $this->success('添加成功','jjcc/extension_list');
        }else{
            $this->error('添加失败');
        }
    }

    //更多好玩删除
    public function extension_del($id)
    {
        $res = db('jjcc_game')->delete($id);
        
        if ($res) {
            $this->success('删除成功','jjcc/extension_list');
        }else{
            $this->error('删除失败');
        }
    }

    //更多好玩编辑页面
    public function extension_edit($id)
    {
        $res = db('jjcc_game')->where(['admin_id'=>$this->accountId,'id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //更多好玩编辑提交
    public function extension_edit_post()
    {
        $post = request()->only(['id','name','type','title','content','url'],'post');
        
        $post['create_time'] = date("Y-m-d");
        $post['admin_id'] = $this->accountId;
        //图片上传
        
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['image'] = $image;
        }
        $where['admin_id'] = $this->accountId;
        $where['id'] = $post['id'];
        
        $res = db('jjcc_game')->where($where)->update($post);
        
        if ($res) {
            $this->success('编辑成功','jjcc/extension_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //上下架游戏
    public function extension_up($id,$type)
    {
        $res = db('jjcc_game')->where(['admin_id'=>$this->accountId,'id'=>$id])->update(['sort'=>$type]);
    
        if ($res) {
            $this->success('操作成功','jjcc/extension_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    //广告游戏跳转统计
    public function statistics_list()
    {
        $post = request()->only(['title'],'post');
        if (!empty($post['title'])){
            $where['game_id'] = $post['title'];
        }
        $where['admin_id'] = $this->accountId;
        
        $res = db('jjcc_game_log')->field('count(id) as number,create_time')->where($where)->group('create_time')->paginate($this->page, false);
        
        $info = db('jjcc_game')->field('name,id')->where(['admin_id'=>$this->accountId])->select();
        
        $page = $res->render();
        $this->assign('info',$info);
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
        

    }
    
    
    //题目管理
    public function topic_list()
    {
        $res = db('jjcc_subject')->where(['admin_id'=>$this->accountId])->order('type asc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    
    //增加题目
    public function topic_add()
    {
        return $this->fetch();
    }
    
    //增加题目提交
    public function topic_add_post()
    {
        $post = request()->only(['topic1','topic2','topic3','answer','type'],'post');
        
        $post['admin_id'] = $this->accountId;
        $post['create_time'] = date("Y-m-d H:i:s");
        
        $res = db('jjcc_subject')->insert($post);
        
        if ($res) {
            $this->success('操作成功','jjcc/topic_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    //编辑题目页面
    public function topic_edit($id)
    {
        $res = db('jjcc_subject')->where(['id'=>$id])->find();
        

        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //编辑题目提交
    public function topic_edit_post()
    {
        $post = request()->only(['id','topic1','topic2','topic3','answer','type'],'post');
        
        $res = db('jjcc_subject')->where(['id'=>$post['id']])->update($post);
        
        if ($res) {
            $this->success('操作成功','jjcc/topic_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    
    
    
    
    
  
    
}